package com.sugon.ohdfs.integration.flink;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.*;

public class SpringBootEnvBuilder {

    static SpringApplication springBootApplication = null;
    static SpringApplicationBuilder springApplicationBuilder = null;

    public static synchronized void run(String[] args){
        if (springBootApplication == null) {
            StandardEnvironment standardEnvironment = new StandardEnvironment();
            MutablePropertySources propertySources = standardEnvironment.getPropertySources();
            propertySources.addFirst(new SimpleCommandLinePropertySource(args));
            String startJarPath = SpringBootEnvBuilder.class.getResource("/").getPath().split("!")[0];
            String[] activeProfiles = standardEnvironment.getActiveProfiles();
            propertySources.addLast(new MapPropertySource("systemProperties", standardEnvironment.getSystemProperties()));
            propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", standardEnvironment.getSystemEnvironment()));
            if (springBootApplication == null) {
                springApplicationBuilder = new SpringApplicationBuilder(SpringBootEnvBuilder.class);
                // 这里可以通过命令行传入
                springApplicationBuilder.profiles("dev");
                springApplicationBuilder.sources(SpringBootEnvBuilder.class).web(WebApplicationType.NONE);
            }
            springBootApplication = springApplicationBuilder.build();
            springBootApplication.run(args);
        }
    }

}
